package com.gcb.controller.crew;

import com.gcb.common.annotation.Log;
import com.gcb.common.core.domain.PageResult;
import com.gcb.common.core.domain.R;
import com.gcb.common.core.page.TableDataInfo;
import com.gcb.common.utils.poi.ExcelUtil;
import com.gcb.controller.BaseController;
import com.gcb.domain.dto.CrewQueryDTO;
import com.gcb.domain.dto.CrewSaveDTO;
import com.gcb.domain.vo.CrewVO;
import com.gcb.framework.utils.SecurityUtils;
import com.gcb.service.ICrewService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/crew")
public class CrewController {

    @Resource
    private ICrewService crewService;

    /**
     * 301.船员列表接口
     *
     * @param dto
     * @return
     */
    @PreAuthorize("@ss.hasPermi('platform:crew:page')")
    @GetMapping(value = "/page")
    public PageResult getPageList(CrewQueryDTO dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<CrewVO> list = crewService.pageList(dto);
        return PageResult.ok(list);
    }

    /**
     * 302.新增船员
     *
     * @param dto
     * @return
     */
    @Log
    @PreAuthorize("@ss.hasPermi('platform:crew:add')")
    @PostMapping(value = "/add")
    public R<String> addCrew(@RequestBody CrewSaveDTO dto) {
        dto.setOperatorId(SecurityUtils.getUserId());
        crewService.add(dto);
        return R.ok();
    }

    /**
     * 306.编辑船员
     *
     * @param dto
     * @return
     */
    @Log
    @PreAuthorize("@ss.hasPermi('platform:crew:edit')")
    @PostMapping(value = "/edit")
    public R<String> updateCrew(@RequestBody CrewSaveDTO dto) {
        crewService.update(dto);
        return R.ok();
    }

    /**
     * 303.船员详情
     *
     * @param id id
     * @return
     */
    @PreAuthorize("@ss.hasPermi('platform:crew:detail')")
    @GetMapping(value = "/detail")
    public R<CrewVO> getCrewDetail(@RequestParam(name = "id") Long id) {
        CrewVO vo = crewService.detail(id);
        return R.ok(vo);
    }

    /**
     * 307.根据用户id获取船员信息
     *
     * @param userId
     * @return
     */
    @PreAuthorize("@ss.hasPermi('platform:crew:getByUserId')")
    @GetMapping(value = "/by/userId")
    public R<CrewVO> getByUserId(@RequestParam(name = "userId") Long userId) {
        CrewVO vo = crewService.getByUserId(userId);
        return R.ok(vo);
    }

    /**
     * 305.已认证船员下拉列表接口
     *
     * @return
     */
    @GetMapping(value = "/list")
    public R<List<CrewVO>> getCrewTreeList() {
        List<CrewVO> list = crewService.pageList(null);
        return R.ok(list);
    }

    /**
     * 304.船员导出
     *
     * @param dto
     * @return
     */
    @Log
    @PreAuthorize("@ss.hasPermi('platform:crew:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, CrewQueryDTO dto) {
        List<CrewVO> vo = crewService.pageList(dto);
        ExcelUtil<CrewVO> util = new ExcelUtil<>(CrewVO.class);
        util.exportExcel(response, vo, "船员列表");
    }

    /**
     * 确认认证状态
     * @param mobile
     * @return
     */
    @GetMapping("/auth/check")
    public R<Boolean> checkAuth(@RequestParam(name = "mobile") String mobile){
        return R.ok(crewService.checkAuth(mobile));
    }
}
